package com.learncloud.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.learncloud.common.core.Constant;
import com.learncloud.common.core.exception.ServiceException;
import com.learncloud.common.core.wrapper.ResponseWrap;
import com.learncloud.common.core.wrapper.ResponseWrapUtil;
import com.learncloud.entity.SysMenu;
import com.learncloud.entity.vo.MenuDetailVo;
import com.learncloud.service.ISysMenuService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 系统菜单表 前端控制器
 * </p>
 *
 * @author yj
 */
@RestController
@RequestMapping("/admin/sys-menu")
public class SysMenuController {

    @Autowired
    private ISysMenuService iSysMenuService;

    @PostMapping("/add")
    @ApiOperation(value = "新增菜单")
    public ResponseWrap<SysMenu> add(@RequestBody SysMenu sysMenu){
        boolean save = iSysMenuService.add(sysMenu);
        return ResponseWrapUtil.success();
    }

    @PostMapping ("/update")
    @ApiOperation(value = "修改菜单")
    public ResponseWrap<SysMenu> update(@RequestBody SysMenu sysMenu){
        if (Constant.Status.DISABLE.value.equals(sysMenu.getStatus())){
            List<SysMenu> list = iSysMenuService.list(new QueryWrapper<>(new SysMenu().setParentId(sysMenu.getId()).setStatus(Constant.Status.ENABLE.value)));
            if (list.size() > 0){
                throw new ServiceException("禁用失败,请先禁用其子菜单");
            }
        }
        boolean update = iSysMenuService.updateById(sysMenu);
        return ResponseWrapUtil.success();
    }

    @DeleteMapping("/del")
    @ApiOperation(value = "删除菜单")
    public ResponseWrap<SysMenu> del(Integer id){
        boolean b = iSysMenuService.del(id);
        return ResponseWrapUtil.success();
    }

    @GetMapping("/getDetails")
    @ApiOperation(value = "获取菜单详情")
    public ResponseWrap<MenuDetailVo> getDetails(Integer id){
        MenuDetailVo byId = iSysMenuService.getDetails(id);
        return ResponseWrapUtil.success(byId);
    }

    @GetMapping("/getMenuDetailVoList")
    @ApiOperation(value = "条件查询菜单列表")
    public ResponseWrap<List<MenuDetailVo>> getMenuDetailVoList(SysMenu sysMenu){
        List<MenuDetailVo> list = iSysMenuService.getMenuDetailVoList(sysMenu);
        return ResponseWrapUtil.success(list);
    }

    @GetMapping("/getList")
    @ApiOperation(value = "条件查询菜单列表")
    public ResponseWrap<List<SysMenu>> getList(SysMenu sysMenu){
        List<SysMenu> list = iSysMenuService.list(new QueryWrapper<>(sysMenu.setStatus(Constant.Status.ENABLE.value)));
        return ResponseWrapUtil.success(list);
    }


    @GetMapping("/getSelfMenus")
    @ApiOperation(value = "获取当前用户菜单")
    public ResponseWrap<List<MenuDetailVo>> getSelfMenus(){
        List<MenuDetailVo> list = iSysMenuService.getSelfMenus();
        return ResponseWrapUtil.success(list);
    }

    @GetMapping("/getPaginationList")
    @ApiOperation(value = "条件分页查询菜单列表")
    public ResponseWrap<Page> getPaginationList(Integer pageSize, Integer pageIndex, SysMenu sysMenu){
        Page page = iSysMenuService.getPaginationList(new Page(pageIndex, pageSize),sysMenu);
        return ResponseWrapUtil.success(page);
    }

}

